4. Write a program to find the reverse of a string using pointers. #include #define MAX_SIZE 100 // Maximum string size int main() { char initial_string[MAX_SIZE], reverse_string[MAX_SIZE]; char *initial_string_ptr = initial_string; char *reverse_string_ptr = reverse_string; int length = 0; printf("Enter any string: "); gets(initial_string); /* Find length of string */ while(*initial_string_ptr != '\0') { length++; initial_string_ptr++; } /* Store each character from end of original string to reverse string */ while(length >= 0) { *(reverse_string_ptr++) = *(--initial_string_ptr); length--; } *reverse_string_ptr = '\0'; printf("\nOriginal string = %s\n", initial_string); printf("Reverse string = %s\n", reverse_string); return 0; } 5. Write a program to sort an array of integers in ascending order using pointers. Use the "bubble sort" sorting algorithm (it works by repeatedly swapping the adjacent elements if they are in the wrong order). #include #define SIZE 10 int main() { int a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37}; int *array = a; int pass = 0; /* passes counter */ int i = 0; /* comparisons counter */ int hold = 0; /* temporary location used to swap array elements */ printf("Data items in original order\n"); for(i = 0; i < SIZE; i++) { printf("%4d", *(array +i)); } /* bubble sort */ /* loop to control number of passes */ for (pass = 1; pass < SIZE; pass++) { /* loop to control number of comparisons per pass */ for (i = 0; i < SIZE - 1; i++) { /* compare adjacent elements and swap them if first element is greater than second element */ if(*(array + i) > *(array + i + 1)) { hold = *(array + i); *(array + i) = *(array + i + 1); *(array + i + 1) = hold; } } } printf( "\nData items in ascending order\n" ); for(i = 0; i < SIZE; i++) { printf("%4d", *(array + i)); } return 0; } 6. C program to convert string to uppercase using pointers */ #include #include #define MAX_SIZE 100 //Maximum size of the string int main() { char string[MAX_SIZE] = {'\0'}; //Array of char definition and initialization char *str_ptr = string; //Pointer to the array of char /* Input string from user */ printf("Enter the string: "); gets(string); /* Convert to uppercase using ASCII codes*/ while(*str_ptr != '\0') { *str_ptr = (*str_ptr >= 'a' && *str_ptr <= 'z') ? *str_ptr - 32 : *str_ptr; str_ptr++; } /* Output string */ printf("Uppercase string: %s", string); return 0; } 7. C program to toggle case each character of a string using pointers #include #include #define MAX_SIZE 100 //Maximum size of the string int main() { char string[MAX_SIZE] = {'\0'}; //Array of char definition and initialization char *str_ptr = string; //Pointer to the array of char /* Input string from user */ printf("Enter the string: "); gets(string); /* Convert to uppercase using ASCII codes */ while(*str_ptr != '\0') { if(*str_ptr >= 'a' && *str_ptr <= 'z') *str_ptr = *str_ptr - 32; else if(*str_ptr >= 'A' && *str_ptr <= 'Z') *str_ptr = *str_ptr + 32; str_ptr++; } /* Output string */ printf("Uppercase string: %s", string); return 0; } 8. C program to toggle case each character of a string using pointers #include #include #define MAX_SIZE 100 //Maximum size of the string int main() { char string[MAX_SIZE] = {'\0'}; //Array of char definition and initialization char *str_ptr = string; //Pointer to the array of char /* Input string from user */ printf("Enter the string: "); gets(string); /* Convert to uppercase using ASCII codes */ while(*str_ptr != '\0') { if(*str_ptr >= 'a' && *str_ptr <= 'z') *str_ptr = *str_ptr - 32; else if(*str_ptr >= 'A' && *str_ptr <= 'Z') *str_ptr = *str_ptr + 32; str_ptr++; } /* Output string */ printf("Uppercase string: %s", string); return 0; }